﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
//using System.Linq;
using System.Text;
//using System.Threading.Tasks;
using TRicheControl.Comm;

namespace TRicheControl.WinformControl
{
    public class RicheTreeNode
    {
        #region Property

        private string mText;
        public string Text
        {
            get
            {
                return mText;
            }
            set
            {
                mText = value;
            }
        }

        private Font mFont;
        public Font Font
        {
            get
            {
                return mFont;
            }
            set
            {
                mFont = value;
            }
        }

        private object mTag;
        public object Tag
        {
            get
            {
                return mTag;
            }
            set
            {
                mTag = value;
            }
        }

        private Rectangle mBounds;
        internal Rectangle Bounds
        {
            get
            {
                if (mBounds == Rectangle.Empty)
                {
                    mBounds = new Rectangle();
                }
                return mBounds;
            }
            set
            {
                mBounds = value;
            }
        }

        private Rectangle mRectangle;

        internal Rectangle Rectangle
        {
            get
            {
                if (mRectangle == Rectangle.Empty)
                {
                    mRectangle = this.mBounds;
                }
                return mRectangle;
            }
            set
            {
                mRectangle = value;
            }
        }

        private RicheTreeNode mParentNode;
        /// <summary>
        /// 父节点
        /// </summary>
        public RicheTreeNode ParentNode
        {
            get
            {
                return mParentNode;
            }
            internal set
            {
                if (mOnParentNodeChange != null && (mParentNode == null || mParentNode != value))
                {
                    mOnParentNodeChange(this);
                }
                mParentNode = value;
            }
        }

        private RicheTreeNode mBeforeNode;
        /// <summary>
        /// 前一个节点
        /// </summary>
        public RicheTreeNode BeforeNode
        {
            get
            {
                return mBeforeNode;
            }
            internal set
            {
                mBeforeNode = value;
            }
        }

        private RicheTreeNode mAfterNode;
        /// <summary>
        /// 后一个节点
        /// </summary>
        public RicheTreeNode AfterNode
        {
            get
            {
                return mAfterNode;
            }
            internal set
            {
                mAfterNode = value;
            }
        }

        private RicheTreeNodeCollection mChildNodes;
        /// <summary>
        /// 子节点集合
        /// </summary>
        public RicheTreeNodeCollection ChildNodes
        {
            get
            {
                if (mChildNodes == null)
                {
                    mChildNodes = new RicheTreeNodeCollection(this);
                }
                return mChildNodes;
            }
            set
            {
                mChildNodes = value;
            }
        }

        private bool mIsShowCheckBox;
        /// <summary>
        /// 是否显示选择框
        /// </summary>
        public bool IsShowCheckBox
        {
            get
            {
                return mIsShowCheckBox;
            }
            set
            {
                mIsShowCheckBox = value;
            }
        }

        private bool mIsCheck;
        /// <summary>
        /// 是否选择
        /// </summary>
        public bool IsCheck
        {
            get
            {
                return mIsCheck;
            }
            set
            {
                mIsCheck = value; 
            }
        }

        private bool mIsExpand;
        /// <summary>
        /// 是否展开
        /// </summary>
        public bool IsExpand
        {
            get
            {
                return mIsExpand;
            }
            set
            {
                mIsExpand = value;
            }
        }

        private bool mIsFocused;
        /// <summary>
        /// 是否获取焦点
        /// </summary>
        public bool IsFocused
        {
            get
            {
                return mIsFocused;
            }
            set
            {
                mIsFocused = value;
            }
        }

        private Bitmap mExpansionImage;
        /// <summary>
        /// 展开时显示的图片
        /// </summary>
        public Bitmap ExpansionImage
        {
            get
            {
                return mExpansionImage;
            }
            set
            {
                mExpansionImage = value;
            }
        }
        private Bitmap mUnExpansionImage;
        /// <summary>
        /// 未展开时显示的图片
        /// </summary>
        public Bitmap UnExpansionImage
        {
            get 
            {
                return mUnExpansionImage;
            }
            set
            {
                mUnExpansionImage = value;
            }
        }

        private Rectangle mImageBounds;

        public Rectangle ImageBounds
        {
            get
            {
                return mImageBounds;
            }
            internal set
            {
                mImageBounds = value;
            }
        }

        private Rectangle mExpanMarkBounds;
        public Rectangle ExpanMarkBounds
        {
            get
            {
                return ExpanMarkBounds;
            }
            set
            {
                ExpanMarkBounds = value;
            }
        }

        #endregion

        internal delegate void ParentNodeChange(RicheTreeNode node);

        private event ParentNodeChange mOnParentNodeChange;
        /// <summary>
        /// 当前该节点的父节点改变后触发的事件
        /// </summary>
        internal event ParentNodeChange OnParentNodeChange
        {
            add { mOnParentNodeChange += value; }
            remove { mOnParentNodeChange -= value; }
        }

        /// <summary>
        /// 展开当前节点
        /// </summary>
        public void Expand()
        {
            Expand(true);
        }

        private void Expand(bool value)
        {
            foreach (RicheTreeNode node in ChildNodes)
            {
                node.IsExpand = value;
            }
        }

        /// <summary>
        /// 展开当前节点并且展开所有子节点
        /// </summary>
        public void ExpandAll()
        {
            ExpandAll(true);
        }

        private void ExpandAll(bool isExpand)
        {
            List<RicheTreeNode> nodes = new List<RicheTreeNode>();
            nodes.AddRange(ChildNodes);
            int count = nodes.Count;
            int index = 0;
            for (; index < count; index++)
            {
                RicheTreeNode node = nodes[index];
                node.IsExpand = isExpand;
                if (node.ChildNodes.Count > 0)
                    continue;
                nodes.AddRange(node.ChildNodes);
                count += node.ChildNodes.Count;
            }
        }
    }

    public class RicheTreeNodeCollection : CollectionBase, IEnumerable<RicheTreeNode>
    {
        private RicheTreeNode mOwnerNode;

        public delegate void ItemChangeEventHandle(ItemChangeType changeType, RicheTreeNode node);

        private event ItemChangeEventHandle mOnItemChangeEvent;

        public event ItemChangeEventHandle OnItemChnageEvent
        {
            add { mOnItemChangeEvent += value; }
            remove { mOnItemChangeEvent -= value; }
        }

        internal RicheTreeNodeCollection(RicheTreeNode ownerNode)
        {
            mOwnerNode = ownerNode;
        }

        public RicheTreeNode this[int index]
        {
            get
            {
                if (index < 0)
                {
                    return null;
                }
                return List[index] as RicheTreeNode; 
            }
            set
            {
                List[index] = value;
            }
        }

        public int Add(RicheTreeNode node)
        {
            if (node == mOwnerNode)
            {
                throw new Exception("不能将本节点作为自己的子节点进行添加");
            }
            node.ParentNode = mOwnerNode;
            int index = List.Add(node);
            if (index != 0)
            {
                node.BeforeNode = this[index - 1];
                node.BeforeNode.AfterNode = node;
            }
            if (mOnItemChangeEvent != null)
            {
                mOnItemChangeEvent(ItemChangeType.Add, node);
            }
            return index;
        }

        public void Remove(RicheTreeNode node)
        {
            node.ParentNode = null;
            if (mOnItemChangeEvent != null)
            {
                mOnItemChangeEvent(ItemChangeType.Remove, node);
            }
            List.Remove(node);
        }

        public int IndexOf(RicheTreeNode node)
        {
            return List.IndexOf(node);
        }

        public new IEnumerator<RicheTreeNode> GetEnumerator()
        {
            foreach (RicheTreeNode node in List)
            {
                yield return node;
            }
        }
    }
}
